<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: index.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
defined('IN_TWCMS') || exit('Access Denied');
header('Content-Type: text/html; charset=utf-8');
if(PHP_VERSION > '5.1') @date_default_timezone_set("Etc/GMT-8");

define('TWCMS_DATA', TWCMS_CORE.'data'.D);				#缓存数据目录
define('TWCMS_CACHE', TWCMS_DATA.'cache'.D);			#数据缓存目录
define('TWCMS_TPLCACHE', TWCMS_DATA.'tplcache'.D);		#主题缓存目录
define('TWCMS_ADMCORE', TWCMS_CORE.'admin'.D);			#后台程序目录
define('TWCMS_FRONT', TWCMS_CORE.'front'.D);			#前台模块目录
define('TWCMS_THEME', TWCMS_CORE.'theme'.D);			#前台主题目录
define('TWCMS_STATIC', 'static/');						#静态文件目录
define('TWCMS_ADMSTATIC', 'static/admin/');				#后台静态文件
define('TWCMS_EDITOR', 'static/editor/');				#编辑器目录
define('TWCMS_UPLOAD', 'upload/');						#上传文件目录
define('P', '-');										#伪静态分隔符

require TWCMS_CORE.'version.php';
require base::load_sys_file('global', 'func');
require base::load_sys_file('db');
if(!file_exists(TWCMS_DATA.'config.inc.php')) jsjump(TWCMS_DIR.'/install/');
require TWCMS_DATA.'config.inc.php';

if(get_magic_quotes_gpc()) {
	$_GET=_stripslashes($_GET);
	$_POST=_stripslashes($_POST);
	$_COOKIE=_stripslashes($_COOKIE);
}

if(!empty($_GET['plugin'])) {
	$pluginName = str_preg($_GET['plugin']);
	$pluginFile = TWCMS_CORE.'plugin'.D.$pluginName.D.'index.php';
	if(file_exists($pluginFile)){
		require $pluginFile;
	}else{
		exit('插件不存在: '.$pluginName);
	}
}elseif(defined('INADMIN')) {
	require TWCMS_ADMCORE.'admin.php';

	empty($_GET['mod']) && $_GET['mod'] = 'admin';
	$cp_mods = array('my', 'setting', 'category', 'content', 'collection', 'theme', 'plugin', 'user', 'tool');

	if($_GET['mod'] == 'json') {
		require base::load_sys_file('upload');
		require base::load_sys_file('jsonUp');
		$classname = 'jsonUp';
	}elseif($_GET['mod'] == 'sqlrevert') {
		require base::load_sys_file('sqlrevert');
		$classname = 'sqlrevert';
	}elseif(in_array($_GET['mod'], $cp_mods)){
		empty($_GET['action']) && $_GET['action'] = 'show';
		$classname = in_array($_GET['mod'], array('my', 'setting')) ? $_GET['mod'] : $_GET['mod'].'_'.$_GET['action'];
		$classfile = TWCMS_CORE.'admin'.D.$classname.'.php';
		if(file_exists($classfile)){
			require $classfile;
		}else{
			exit('模块不存在: '.$classname);
		}
	}elseif(in_array($_GET['mod'], array('logout', 'login', 'admin'))){
		$classname = 'admin';
	}else{
		exit('非法访问!');
	}

	$adm = new $classname();
	echo '<!-- Exetime:'.(microtime(true)-$starttime).', Querynum:'.$adm->db->querynum.', Memory:'.get_byte(memory_get_usage()-$memory).' -->';
}else{
	require base::load_sys_file('front', 'func');
	require base::load_sys_file('tpl');
	require TWCMS_FRONT.'index.php';
	echo '<!-- Exetime:'.(microtime(true)-$starttime).', Querynum:'.(empty($_ENV->db)?0:$_ENV->db->querynum).', Memory:'.get_byte(memory_get_usage()-$memory).' -->';
}

class base{
	public $db;
	public $G;

	public function load_db(){
		$this->connect();
		$this->G = $this->load_cache('setting');
	}

	private function connect(){
		$this->db = new DB;
		$isdb = @$this->db->connect(TW_DBHOST, TW_DBUSER, TW_DBPW, TW_DBNAME, TW_DBCHARSET, TW_PCONNECT, TW_DBPRE);
		if($isdb != 'ok') {
			if($isdb == 'error_connect') {
				loadErr("连接数据库出错！请查看 config.inc.php 文件中的用户名和密码是否正确？");
			}elseif($isdb == 'error_select') {
				loadErr("数据库不存在！请查看 config.inc.php 文件中的数据库名称是否填写正确？");
			}
		}
	}

	public function get_setting(){
		$arr = array();
		$query = $this->db->query('SELECT kay,value FROM `'.TW_DBPRE.'setting`');
		while($data = $this->db->fetch_array($query)) {
			$arr[$data['kay']] = $data['value'];
		}
		return $arr;
	}

	public function getModelId() {
		$query = $this->db->query('SELECT mid,tablename FROM `'.TW_DBPRE.'model` WHERE mid>1');
		$arr = array();
		while($row = $this->db->fetch_array($query)) {
			$arr[$row['mid']] = $row['tablename'];
		}
		return $arr;
	}

	/**
	 * 载入缓存数据
	 * @param string $cachename 缓存文件名
	 */
	public function load_cache($cachename) {
		$cachefile = TWCMS_CACHE.$cachename.'.php';
		if(file_exists($cachefile)) {
			return include $cachefile;
		}else{
			$func = 'get_'.$cachename;
			$arr = $this->$func();
			if($cachename == 'setting') {
				empty($arr['theme']) && $arr['theme'] = 'default';
				$arr['tpl'] = $arr['webdir'].TWCMS_DIR.'/theme/'.$arr['theme'].'/';
				$arr['weburl'] = 'http://'.$arr['webdomain'].$arr['webdir'];
				$arr['modelId'] = $this->getModelId();
				$arr['modelKey'] = array_flip($arr['modelId']);
			}
			f_write($cachefile, '<?php return '.var_export($arr, true).';');
			return $arr;
		}
	}

	/**
	 * 加载系统文件
	 * @param string $filename 文件名
	 * @param string $type 文件类型
	 */
	public static function load_sys_file($filename, $type='class') {
		switch($type) {
			case 'class':
				return TWCMS_CORE.'lib'.D.'class'.D.$filename.'.class.php';
			case 'func':
				return TWCMS_CORE.'lib'.D.'function'.D.$filename.'.func.php';
			case 'res':
				return TWCMS_CORE.'lib'.D.'resources'.D.$filename;
			case 'twcms':
				return TWCMS_CORE.'lib'.D.'twcms'.D.$filename.'.php';
			default:
				return TWCMS_CORE.'lib'.D.'class'.D.$filename.'.class.php';
		}
	}
}